/**
 * Created by 30947 on 2018/7/20.
 */
$(function(){
    getHt();
    initMap();
    mapActive();

})

// 获取 div 的高度
function getHt(){
    var all_height=$(window).height();
    var div_height=all_height-84;
    $("#car_control").css("height",div_height+"px");
}

// 加载地图
function initMap(){
    map = L.map('map_box', {
        center: [30.53786, 114.365248],
        zoom: 10,
        crs: L.CRS.EPSG3857
    });

    // 添加天地图图层
    url = "http://120.55.97.208:8090/iserver/services/map-tianditu/rest/maps/Vector%20Base%20Map%20_%20Mercator";
    new L.supermap.TiledMapLayer(url).addTo(map);

    url = "http://120.55.97.208:8090/iserver/services/map-tianditu/rest/maps/Vector%20Chinese%20Notes%20_%20Mercator";
    new L.supermap.TiledMapLayer(url).addTo(map);

// 热力半径
    var heatRadius = 30;
// 加热力坐标信息数组
    var heatPoints = [];
    //点聚合图层
    makerClusterLayer = L.markerClusterGroup({
        spiderfyOnMaxZoom: false,
        // 设置为true时显示聚类所占据的范围
        showCoverageOnHover: false,
        // 设置为true时会向低一级聚类缩放
        zoomToBoundsOnClick: false
    });
    //maker图层
    var makerLayer = L.layerGroup();
    //获取求助点数据
    fetch("/relation/getallhelpinfo", {
        method: 'GET'
    })
        .then(response => {
            if (!response.ok) {
                throw new Error(`HTTP error! status: ${response.status}`);
            }
            return response.json();
        })
        .then(data => {
            //热力图数据处理
            data.forEach(user => {
                heatPoints.push([user.lat, user.lon,1]);
            });
            // 点聚合数据
            heatPoints.forEach(point => {
                var latLng = L.latLng(point[0], point[1]);
                makerClusterLayer.addLayer(L.marker(latLng));
            });
            //标志点处理
            let color = 'red';
            const marker =
            data.forEach(user => {
                const marker = L.circleMarker([user.lat, user.lon], {color}).addTo(makerLayer);
                marker.bindPopup(`盲人ID: ${user.bid}<br>志愿者ID: ${user.vid}<br>求助时间: ${user.btime}<br>结束时间: ${user.etime}<br>位置: ${user.address}`);
            });
        })
        .catch(error => {
            console.error("Error fetching data:", error);
        });

// 调用渲染方法，传入半径，透明度参数，添加到地图，热力图
    var resultLayer = L.heatLayer(heatPoints, {
        radius: heatRadius,
        minOpacity: 0.5
    });
    var baseMaps = { "热力图": resultLayer ,"点聚合":makerClusterLayer,"标记点":makerLayer};

// 添加图层切换控件
    L.control.layers(baseMaps).addTo(map);
    // L.control.layers(ditu, resultLayer).addTo(map);


}

// 工具条点击效果
function mapActive(){
    $(".map_top>ul>li").click(function(){
        $(this).addClass("active").siblings().removeClass("active");
        $(this).find("a").addClass("active");
        $(this).find("a").parents("li").siblings().find("a").removeClass("active");
    })
}



